<template>
	<view class="p60">
		<view class="title">
			注册信息
		</view>

		<view class="input-phone">
			<input v-model="formData.name" class="uni-input" :placeholder="namePlaceHolder" />
		</view>

		<view class="line" />

		<view class="input-phone">
			<input v-model="formData.work" class="uni-input" :placeholder="workPlaceHolder" />
		</view>

		<view class="line" />

		<view class="input-phone">
			<input type="number" v-model="formData.job" class="uni-input" :placeholder="jobPlaceHolder" />
		</view>

		<view class="line" />

		<view class="input-phone">
			<input v-model="formData.phone" class="uni-input" confirm-type="search" placeholder="请输入您的手机号" />
		</view>

		<view class="line">

		</view>

		<view class="input-code-box">
			<view class="input-code">
				<input v-model="formData.code" class="uni-input" confirm-type="search" placeholder="请输入验证码" />
			</view>

			<view class="btn-send" @click="sendPhoneMsg">
				{{countdown}}
			</view>
		</view>

		<view class="line">

		</view>

		<button @getuserinfo="getuserinfo" class="btn-submit" open-type="getUserInfo">
			提交
		</button>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				formData: {
					phone: null,
					code: null,
					name: null,
					work: null,
					job: null
				},
				timer: null,
				countdown: '获取验证码',
				disabled: false
			}
		},
		onLoad() {
			if (this.userInfo !== null) {
				this.formData.name = this.userInfo.name;
				this.formData.work = this.userInfo.company;
				this.formData.phone = this.userInfo.phone;
				this.formData.job = this.userInfo.job;

			}
		},
		computed: {
			...mapState('userStore', ['hasLogin', 'userInfo']),
			jobPlaceHolder() {
				return '请输入您的职务';
			},
			workPlaceHolder() {
				return '请输入您的工作单位';
			},
			namePlaceHolder() {
				return '请输入您的姓名';
			}
		},
		onLoad() {
			this.silentLogin(params => {
				this.setCode(params);
			})
		},
		methods: {
			...mapMutations('userStore', ['saveUserInfo']),
			setCode(params) {
				this.$request.get({
					url: 'setWXCode',
					data: params,
				}).then(info=>{
					this.saveUserInfo(info);
				},res=>{});
			},
			silentLogin(cb) {
				let that = this;
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						uni.getUserInfo({
							provider: 'weixin',
							success: function(infoRes) {
								that.rawData = JSON.parse(infoRes.rawData);
								if (cb) {
									const params = {
										...JSON.parse(infoRes.rawData),
										code: loginRes.code
									}
									cb(params);
								}
							}
						});
					}
				});
			},
			getuserinfo(e) {
				let that = this;
				this.silentLogin(params => {
					this.setCode(params);
					this.submit();
				})
			},
			sendPhoneMsg() {
				if (this.disabled) return;
				let that = this;
				this.$request.get({
					url: 'sendPhoneMsg',
					data: {
						phone: this.formData.phone
					},
				}).then(res => {
					that.countdown = 60;
					that.timer = setInterval(that.countDown, 1000);
				}, res => {});
			},
			countDown() {
				var that = this;
				if (!that.countdown) {
					that.disabled = false;
					that.timestatus = false;
					that.countdown = '获取验证码';
					clearInterval(that.timer);
				} else {
					that.disabled = true;
					--that.countdown;
				}
			},
			verifyFormData(cb) {
				if (this.formData.name === null) {
					uni.showToast({
						title: '请输入姓名',
						duration: 2000,
						icon: 'none'
					})
					return;
				}

				if (this.formData.work === null) {
					uni.showToast({
						title: '请输入工作单位',
						duration: 2000,
						icon: 'none'
					})
					return;
				}
				if (this.formData.job === null) {
					uni.showToast({
						title: '请输入职务',
						duration: 2000,
						icon: 'none'
					})
					return;
				}
				if (this.formData.phone === null) {
					uni.showToast({
						title: '请输入手机号',
						duration: 2000,
						icon: 'none'
					})
					return;
				}

				if (this.formData.code === null) {
					uni.showToast({
						title: '请输入验证码',
						duration: 2000,
						icon: 'none'
					})
					return;
				}
				cb && cb();

			},
			submit() {
				this.verifyFormData(() => {
					this.$request.get({
						url: 'insertUser',
						data: this.formData,
						loadingTip: '正在处理...'
					}).then(res => {
						uni.showToast({
							title: '信息更新成功！',
							duration: 2000,
							icon: 'none'
						});
						this.saveUserInfo(res.data);
						setTimeout(() => {
							uni.navigateBack({
								
							});
						}, 1000);
					}, res => {});
				});

			},
			phoneChange(e) {
				this.formData.phone = e.detail.value;
			},
			codeChange(e) {
				this.formData.code = e.detail.value;
			}
		}
	}
</script>

<style>
	.btn-send {
		color: #268CFF;
		font-size: 32upx;
	}

	.p60 {
		padding: 0 60upx 40upx 60upx;
	}

	.input-code-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		margin-top: 80upx;
	}

	.input-phone {
		margin-top: 100upx;
	}

	.line {
		width: 100%;
		height: 2upx;
		background: rgba(239, 239, 239, 1);
		box-shadow: 0px 6upx 24upx 0upx rgba(56, 73, 184, 0.04);
		border-radius: 16upx;
		margin: 20upx auto;
	}

	.btn-submit {
		margin-top: 180upx;
		width: 620upx;
		height: 98upx;
		background: linear-gradient(183deg, rgba(11, 200, 253, 1) 0%, rgba(4, 151, 250, 1) 100%);
		border-radius: 50upx;

		color: #FFFFFF;
		font-size: 36upx;
		line-height: 98upx;
		text-align: center;
	}

	.uni-input {
		font-size: 32upx;
		color: #333333;
	}

	.title {
		font-size: 48upx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: rgba(72, 82, 107, 1);
		margin-top: 120upx;
	}
</style>
